package com.rayson.yututu.controller;

import cn.hutool.core.util.StrUtil;
import com.rayson.yututu.common.BaseResponse;
import com.rayson.yututu.exception.ErrorCodeEnum;
import com.rayson.yututu.exception.ThrowUtils;
import com.rayson.yututu.manager.auth.annotation.SaSpaceCheckPermission;
import com.rayson.yututu.manager.auth.model.SpaceUserPermissionConstant;
import com.rayson.yututu.model.entity.SpaceUser;
import com.rayson.yututu.model.entity.User;
import com.rayson.yututu.model.req.IdRequest;
import com.rayson.yututu.model.req.spaceUser.SpaceUserAddReq;
import com.rayson.yututu.model.req.spaceUser.SpaceUserEditReq;
import com.rayson.yututu.model.req.spaceUser.SpaceUserQueryReq;
import com.rayson.yututu.model.vo.spaceUser.SpaceUserVO;
import com.rayson.yututu.service.SpaceUserService;
import com.rayson.yututu.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author: rayson
 * Description:
 * Date: 2025-01-18 22:40
 */
@RestController
@RequestMapping("/spaceUser")
public class SpaceUserController {

    @Resource
    private SpaceUserService spaceUserService;

    @Resource
    private UserService userService;

    /**
     * 添加空间成员
     */
    @PostMapping("/add")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<String> addSpaceUser(@RequestBody @Valid SpaceUserAddReq spaceUserAddReq) {
        return BaseResponse.success(spaceUserService.addSpaceUser(spaceUserAddReq));
    }

    /**
     * 移除空间成员
     */
    @PostMapping("/delete")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> deleteSpaceUser(@RequestBody IdRequest idRequest) {
        if (StrUtil.isBlank(idRequest.getId())) {
            return BaseResponse.fail("id不能为空");
        }
        Boolean result = spaceUserService.deleteSpaceUser(idRequest);
        return BaseResponse.success(result);
    }

    /**
     * 查询空间成员
     */
    @PostMapping("/get")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<SpaceUser> getSpaceUser(@RequestBody SpaceUserQueryReq queryReq) {
        String userId = queryReq.getUserId();
        String spaceId = queryReq.getSpaceId();
        ThrowUtils.throwIf(StrUtil.hasEmpty(userId, spaceId), ErrorCodeEnum.PARAM_ERROR);
        SpaceUser spaceUser = spaceUserService.getOne(spaceUserService.getQueryWrapper(queryReq));
        ThrowUtils.throwIf(Objects.isNull(spaceUser), ErrorCodeEnum.NOT_FOUND_ERROR);
        return BaseResponse.success(spaceUser);
    }

    /**
     * 查询空间成员列表
     */
    @PostMapping("/list")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<List<SpaceUserVO>> listSpaceUserVO(@RequestBody SpaceUserQueryReq queryReq) {
        List<SpaceUser> spaceUserList = spaceUserService.list(spaceUserService.getQueryWrapper(queryReq));
        return BaseResponse.success(spaceUserService.getSpaceUserVOList(spaceUserList));
    }

    /**
     * 编辑成员
     */
    @PostMapping("/edit")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Void> editSpaceUser(@RequestBody @Valid SpaceUserEditReq editReq) {
        spaceUserService.editSpaceUser(editReq);
        return BaseResponse.success(null);
    }

    /**
     * 查询我加入的团队空间
     */
    @PostMapping("/list/my")
    public BaseResponse<List<SpaceUserVO>> listMyTeamSpaceUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        SpaceUserQueryReq queryReq = new SpaceUserQueryReq();
        queryReq.setUserId(loginUser.getId());
        List<SpaceUser> spaceUserList = spaceUserService.list(spaceUserService.getQueryWrapper(queryReq));
        return BaseResponse.success(spaceUserService.getSpaceUserVOList(spaceUserList));
    }
}
